package com.shijie.component.logistic;

import com.shijie.core.ResultCode;
import com.shijie.core.ServiceException;
import com.shijie.utils.HttpUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 物流接口查询
 *
 * https://market.aliyun.com/products/57126001/cmapi010996.html?spm=5176.2020520132.101.5.60hxIs#sku=yuncode499600008
 *
 * Created by yanqiu on 2018/7/31.
 */
@Component
public class LogisticService {

    @Autowired
    LogisticConfig logisticConfig;

    public String logisticMessage(String com, String logisticNo) {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + logisticConfig.getAppCode());
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("com", com);
        querys.put("nu", logisticNo);

        try {
            HttpResponse response = HttpUtil.doGet(logisticConfig.getHost(), logisticConfig.getPath(), headers, querys);
            //TODO 放入缓存，取缓存的数据
            return EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            throw new ServiceException(ResultCode.FAIL, e.getMessage());
        }
    }
}
